package gui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

public class ColorPalette extends Observable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<JPanel> colors;
	private final int COLUMNS = 2;
	private final int SIZE = 20;
	private Color current;
	private JPanel palette;



	public ColorPalette(Observer obs) {
		super();
		this.addObserver(obs);
		ArrayList<Color> colors = new ArrayList<Color>();
		colors.add(Color.BLACK);
		colors.add(Color.BLUE);
		colors.add(Color.CYAN);
		colors.add(Color.GREEN);
		colors.add(Color.MAGENTA);
		colors.add(Color.RED);
		colors.add(Color.YELLOW);
		colors.add(Color.WHITE);
		initComponents(colors);
	}


	private void initComponents(List<Color> flawors) {
		this.palette = new JPanel();
		
		this.current = new Color(0);
		
		this.colors = new ArrayList<JPanel>();
		
		for (int i = 0; i < flawors.size(); i++) {
			JPanel panel = new JPanel(null);
			panel.setBackground(flawors.get(i));
			panel.setBorder(BorderFactory.createLineBorder(Color.WHITE));
			panel.addMouseListener(mouseListener(panel));
			this.colors.add(panel);
		}
		
		this.palette.setLayout(new GridLayout(0,(int)colors.size()/COLUMNS));
		
		this.palette.setSize(SIZE*((int)colors.size()/COLUMNS), SIZE*(colors.size()/(((int)colors.size()/COLUMNS))));
		
		for (int i = 0; i < this.colors.size(); i++) 
		{
			this.palette.add(colors.get(i));
		}
		
	}
	
	private MouseListener mouseListener(final JPanel panel) {
		MouseListener ml = new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {}
			
			@Override
			public void mousePressed(MouseEvent e) {}
			
			@Override
			public void mouseExited(MouseEvent e) {}
			
			@Override
			public void mouseEntered(MouseEvent e) {}
			
			@Override
			public void mouseClicked(MouseEvent e) 
			{
				current = panel.getBackground();
				setChanged();
			    notifyObservers();

			}
		};
		return ml;
	}

	public Color getCurrent() {
		return current;
	}


	public void setCurrent(Color current) {
		this.current = current;
	}

	public JPanel getPalette() {
		return palette;
	}

	public void setPalette(JPanel palette) {
		this.palette = palette;
	}

	
}
